/**
 **************************************************************************
 * @file     state_led.c
 * @brief    state led
 **************************************************************************
 *
 *
 **************************************************************************
 *
 */
#include "state_led.h"
#include "at32f413.h"

extern LED state_led;

void LED::init(void)
{

    crm_periph_clock_enable(m_crm_clock, TRUE);

    gpio_default_para_init(&m_gpio_init_struct);

    m_gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
    m_gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
    m_gpio_init_struct.gpio_mode = GPIO_MODE_OUTPUT;
    m_gpio_init_struct.gpio_pins = m_pin;
    m_gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
    gpio_init(m_gpiox, &m_gpio_init_struct);
}

LED::LED(gpio_type* _gpiox, u32 _pin, crm_periph_clock_type _crm_clock)
{
    m_gpiox = _gpiox;
    m_pin = _pin;
    m_crm_clock = _crm_clock;
}

void LED::off(void)
{
    m_gpiox->odt |= m_pin;
}

void LED::on(void)
{
    m_gpiox->clr = m_pin;
}

void LED::toggle(void)
{
    m_gpiox->odt ^= m_pin;
}

/**
 * @brief 基于滴答定时器的LED闪烁函数
*/
void led_spark(LED& led)
{
    static __IO uint32_t timingdelaylocal = 0;

    // timingdelaylocal 不等于 0
    if (timingdelaylocal)
    {

        if (timingdelaylocal < 500)
        {
            state_led.on();
        }
        else
        {
            state_led.off();
        }

        timingdelaylocal--;
    }
    else
    { // timingdelaylocal 等于 0
        timingdelaylocal = 1000;
    }
}
